<?php
/**
 * about business basic message
 * edit by kim
 */
class b2c_ctl_admin_operator_member extends desktop_controller{
	var $workground = 'b2c_ctl_admin_operator';
	public function __construct($app)
    {
        parent::__construct($app);
		header("cache-control: no-store, no-cache, must-revalidate");
    }

//    function index(){
//        if($_GET['action'] == 'export') $this->_end_message = '导出会员';
//        //增加会员相关权限判断@lujy
//        if($this->has_permission('addmember')){
//           $custom_actions[] = array('label'=>app::get('b2c')->_('添加集货员'),'href'=>'index.php?app=b2c&ctl=admin_operator_member&act=add_operator','target'=>'dialog::{title:\''.app::get('b2c')->_('添加集货员').'\',width:500,height:350}');
//        }
//        if($this->has_permission('send_email')){
//            $custom_actions[] =  array('label'=>app::get('b2c')->_('群发邮件'),'submit'=>'index.php?app=b2c&ctl=admin_member&act=send_email','target'=>'dialog::{title:\''.app::get('b2c')->_('群发邮件').'\',width:700,height:400}');
//        }
//        if($this->has_permission('send_msg')){
//            $custom_actions[] =  array('label'=>app::get('b2c')->_('群发站内信'),'submit'=>'index.php?app=b2c&ctl=admin_member&act=send_msg','target'=>'dialog::{title:\''.app::get('b2c')->_('群发站内信').'\',width:500,height:350}');
//        }
//        if($this->has_permission('send_sms')){
//            $custom_actions[] =  array('label'=>app::get('b2c')->_('群发短信'),'submit'=>'index.php?app=b2c&ctl=admin_member&act=send_sms','target'=>'dialog::{title:\''.app::get('b2c')->_('群发短信').'\',width:500,height:350}');
//        }
//
//        $actions_base['title'] = app::get('b2c')->_('集货员列表');
//        $actions_base['actions'] = $custom_actions;
//        $actions_base['allow_detail_popup'] = true;
//        $actions_base['use_buildin_export'] = false;
//        $actions_base['use_buildin_set_tag'] = true;
//        $actions_base['use_buildin_filter'] = true;
//        $actions_base['use_buildin_recycle'] = false;
//        $actions_base['finder_aliasname'] ='companyindex';
//        $actions_base['base_filter'] = array('user_type'=>2);
//
//        $actions_base['use_view_tab'] = true;
//        $actions_base['object_method'] =array('count' => 'get_finder_count', 'getlist' => 'get_finder_company');
//        $this -> finder('b2c_mdl_members',$actions_base);
//    }

	//集货员基础信息
	public function index(){
		$this->finder('b2c_mdl_operator_member',array(
            'title'=>app::get('b2c')->_('基础信息'),
            'actions'=>array(
                         array('label'=>app::get('b2c')->_('添加集货员	'),'href'=>'index.php?app=b2c&ctl=admin_operator_member&act=add_operator','target'=>'dialog::{width:600,height:450,title:\''.app::get('b2c')->_('添加集货员').'\'}'),
	                     array('label'=>app::get('b2c')->_('禁用'),'submit'=>'index.php?app=b2c&ctl=admin_operator_member&act=dis_enable','target'=>'refresh'),
	                     array('label'=>app::get('b2c')->_('解禁'),'submit'=>'index.php?app=b2c&ctl=admin_operator_member&act=enable','target'=>'refresh')
                        ),
            'allow_detail_popup'=>true,
            'use_buildin_recycle'=>false,
            ));
	}
    
	//添加集货员操作 @kim
    function add_operator(){
        $member_lv=$this->app->model("member_lv");
        foreach($member_lv->getMLevel() as $row){
            $options[$row['member_lv_id']] = $row['name'];
        }
        $a_mem['lv']['options'] = is_array($options) ? $options : array(app::get('b2c')->_('请添加会员等级')) ;
        $attr = kernel::single('b2c_user_passport')->get_signup_attr();
        $this->pagedata['attr'] = $attr;
        $this->pagedata['mem'] = $a_mem;
		//手机区号前缀
		$phone_setting = $save_data['phone_setting'];
		$phones=app::get('b2c')->getConf('setting-phone.phone');
		$this->pagedata['phones'] = $phones;
        $this->display('admin/operator/new.html');
    }

    //集货员的添加操作 @kim
function add(){
	$this->begin();
	$userPassport = kernel::single('b2c_user_passport');

	if( !$userPassport->check_signup_account($_POST['pam_account']['login_name'],$message) ){
		$this->end(false, $message);
	}

	if(!$userPassport->check_passport($_POST['pam_account']['login_password'],$_POST['pam_account']['psw_confirm'],$message)){
		$this->end(false, $message);
	}
      
	$_POST["usertype"] = 2;//集货员:2
	$saveData = $_POST;
	$operatorData = $_POST['operator'];
	$saveData = $userPassport->business_signup_process($saveData);

	//验证手机号及格式
	$mobile = trim($operatorData['tel']);
	$phone_setting = trim($operatorData['phone_setting']);
	$login_type = $userPassport->get_login_account_type($mobile);
	if($login_type != 'mobile'){
		$this->end(false,app::get('b2c')->_('请填写正确的手机号码'));
	}

	//判断手机号位数是否和符合配置区号内容
	$phones=app::get('b2c')->getConf('setting-phone.phone');
	foreach($phones as $v){
		if(in_array($phone_setting,$v)){
			$newarr = $v;
		}
	}
	$len = $newarr['length'];
	if((!preg_match('/^[1-9]\d{'.($len-1).'}$/', $mobile))){
		$this->end(false,app::get('b2c')->_('手机格式不正确'));
	}

	//判断手机号是否已经存在，包括区号相同
	$pamMembersModel = app::get('pam')->model('members');
	$list = $pamMembersModel -> getRow('phone_setting',array('login_account'=>$mobile,'login_type'=>'mobile'));
	$prefix = $list['phone_setting'];
	if($list || $phone_setting == $prefix){
		$this->end(false,app::get('b2c')->_('手机号码已存在'));
	}

	if( !$member_id = $userPassport->save_operator($saveData,$operatorData,$msg) ){
		$this->end(false, app::get('b2c')->_('添加失败！'));
	}
      //增加会员同步 2012-5-15
      if( $member_rpc_object = kernel::service("b2c_member_rpc_sync") ) {
          $member_rpc_object->createActive($member_id);
      }
      $this->end(true, app::get('b2c')->_('添加成功！'));
    }

	//修改供应商的基础信息
    public function edit(){
		$operatorModel = $this->app->model('operator_member');
        $id = trim($_GET['bid']);
        $msg = $operatorModel->getRow('*',array('id'=>$id));
        $this->pagedata['msg'] = $msg;
		//手机区号前缀
		$phone_setting = $save_data['phone_setting'];
		$phones=app::get('b2c')->getConf('setting-phone.phone');
		$this->pagedata['phones'] = $phones;
        $this->singlepage('admin/operator/operator_message.html');
    }

	public function editmsg(){
		$operatorModel = $this->app->model('operator_member');
		$userPassport = kernel::single('b2c_user_passport');
		$this->begin();
		$saveData = $_POST['operator'];
		//验证手机号及格式
		$mobile = trim($saveData['tel']);
		$phone_setting = trim($saveData['phone_setting']);
		$login_type = $userPassport->get_login_account_type($mobile);
		if($login_type != 'mobile'){
			$this->end(false,app::get('b2c')->_('请填写正确的手机号码'));
		}

		//判断手机号位数是否和符合配置区号内容
		$phones=app::get('b2c')->getConf('setting-phone.phone');
		foreach($phones as $v){
			if(in_array($phone_setting,$v)){
				$newarr = $v;
			}
		}
		$len = $newarr['length'];
		if((!preg_match('/^[1-9]\d{'.($len-1).'}$/', $mobile))){
			$this->end(false,app::get('b2c')->_('手机格式不正确'));
		}

		//判断手机号是否已经存在，包括区号相同
		$pamMembersModel = app::get('pam')->model('members');
		$list = $pamMembersModel -> getRow('phone_setting',array('login_account'=>$mobile,'login_type'=>'mobile'));
		$prefix = $list['phone_setting'];
		if($list || $phone_setting == $prefix){
			$this->end(false,app::get('b2c')->_('手机号码已存在'));
		}

		$id = trim($_POST['id']);
		$bool = $operatorModel->update($saveData,array('id'=>$id));
        if($bool){
            $this->end(true, app::get('b2c')->_('修改成功！'));
        }
		$this->end(false, app::get('b2c')->_('修改失败！'));
	}

	//后台集货员手机格式验证 by kim
	function check_tel(){
		$userPassport = kernel::single('b2c_user_passport');
		if( !$userPassport->check_operator_tel(trim($_POST['mobile']),$message,trim($_POST['phone_setting']),trim($_POST['len'])) ){
			echo json_encode(array('error'=>$message));exit;
		}
		echo json_encode(array('success'=>app::get('b2c')->_('该手机可用')));exit;
    }

	//后台供应商验证手机格式
	function check_tel2(){
		$userPassport = kernel::single('b2c_user_passport');
		if( !$userPassport->check_business_tel(trim($_POST['mobile']),$message,trim($_POST['phone_setting']),trim($_POST['len'])) ){
			echo json_encode(array('error'=>$message));exit;
		}
		echo json_encode(array('success'=>app::get('b2c')->_('该手机可用')));exit;
	}

	function enable(){
		if(!$this->has_permission('batcheditmarketable')){
			$this->begin('');
			$this->end(false, app::get('b2c')->_('您无权操作'));
		}


		$this->begin('');
		if($_POST['isSelectedAll'] == '_ALL_'){
			$view_filter = $this->get_view_filter('b2c_ctl_admin_members','b2c_mdl_members');
			$_POST = array_merge($_POST,$view_filter);
			unset($_POST['isSelectedAll']);
		}
		$objMembers = app::get('pam')->model('members');
		$glist = $objMembers->jhy_setEnabled($_POST,'false');
		$this->end(true, app::get('b2c')->_('选中集货员已解禁'));
	}

	function dis_enable(){
		if(!$this->has_permission('batcheditmarketable')){
			$this->begin('');
			$this->end(false, app::get('b2c')->_('您无权操作'));
		}
		$this->begin('');
		if($_POST['isSelectedAll'] == '_ALL_'){
			$view_filter = $this->get_view_filter('b2c_ctl_admin_members','b2c_mdl_members');
			$_POST = array_merge($_POST,$view_filter);
			unset($_POST['isSelectedAll']);
		}
		$objMembers = app::get('pam')->model('members');
		$glist = $objMembers->jhy_setEnabled($_POST,'true',$msg);
		if(!$glist){
			$this->end(false, $msg);
		}else{
			$this->end(true, app::get('b2c')->_('选中集货员已禁用'));
		}
	}

}